package com.zide.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zide.common.constant.UserConstants;
import com.zide.common.core.domain.BaseEntity;
import com.zide.common.core.domain.PageResult;
import com.zide.common.exception.ServiceException;
import com.zide.common.utils.StringUtils;
import com.zide.platform.entity.PlatformPost;
import com.zide.platform.mapper.PlatformPostMapper;
import com.zide.platform.mapper.PlatformUserPostMapper;
import com.zide.platform.service.PlatformPostService;
import com.zide.platform.vo.search.PlatformPostSearchVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author 石添添
 * @description
 * @date 2024-09-23 22:00
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PlatformPostServiceImpl implements PlatformPostService {

    private final PlatformPostMapper postMapper;

    private final PlatformUserPostMapper userPostMapper;

    @Override
    public PageResult<PlatformPost> selectPage(PlatformPostSearchVO searchVO) {

        return postMapper.selectPage(searchVO);
    }

    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<PlatformPost> selectPostList(PlatformPost post) {
        LambdaQueryWrapper<PlatformPost> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(post.getPostCode())) {
            wrapper.like(PlatformPost::getPostCode,post.getPostCode());
        }
        if(StrUtil.isNotEmpty(post.getPostName())) {
            wrapper.like(PlatformPost::getPostName,post.getPostName());
        }
        if(ObjectUtil.isNotEmpty(post.getStatus())) {
            wrapper.eq(PlatformPost::getStatus,post.getStatus());
        }
        wrapper.orderByDesc(BaseEntity::getCreateTime);

        return postMapper.selectList(wrapper);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<PlatformPost> selectPostAll() {
        return postMapper.selectList();
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public PlatformPost selectPostById(Long postId) {
        return postMapper.selectById(postId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return postMapper.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(PlatformPost post) {
        Long postId = StringUtils.isNull(post.getId()) ? -1L : post.getId();
        LambdaQueryWrapper<PlatformPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatformPost::getPostName, post.getPostName());
        PlatformPost info = postMapper.selectOne(wrapper);
        if (StringUtils.isNotNull(info) && info.getId().longValue() != postId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(PlatformPost post) {
        Long postId = StringUtils.isNull(post.getId()) ? -1L : post.getId();
        LambdaQueryWrapper<PlatformPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatformPost::getPostCode, post.getPostCode());
        PlatformPost info = postMapper.selectOne(wrapper);
        if (StringUtils.isNotNull(info) && info.getId().longValue() != postId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId) {
        return userPostMapper.selectCount("post_id",postId).intValue();
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int deletePostById(Long postId) {
        return postMapper.deleteById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deletePostByIds(Long[] postIds) {
        for (Long postId : postIds) {
            PlatformPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return postMapper.deleteBatchIds(Arrays.asList(postIds));
    }

    /**
     * 新增保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(PlatformPost post) {
        return postMapper.insert(post);
    }

    /**
     * 修改保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(PlatformPost post) {
        return postMapper.updateById(post);
    }
}
